<?php
class TicketShowingController extends SeatAppController {
	
	public function admin_index($ticket_id){
		$this->loadModel('Ticket');
		$this->Ticket->recursive = -1;
		$ticket = $this->Ticket->find('first',array(
		 	'fields'=>array('Ticket.id','Ticket.title','Ticket.venue_id'),
		 	'conditions'=>array(
	 			'Ticket.id'=>$ticket_id
 			)
	 	));
 		
		$this->loadModel('Seat.SeatTicket');
		$seatTicket = $this->SeatTicket->findByTicketId($ticket_id);
		if(isset($seatTicket['SeatTicket']['id'])){
			$seat_ticket_id = $seatTicket['SeatTicket']['id'];
		}else{
			$this->loadModel('Seat.ModelX');
			$model = $this->ModelX->findByRelateId($ticket['Ticket']['venue_id']);
			$this->SeatTicket->save(array(
				'model_id'=>$model['ModelX']['relate_id'],
				'ticket_id'=>$ticket_id,
				'sort'=>100,
				'enable'=>1
			));
			$seat_ticket_id = $this->SeatTicket->id;
		}
		$ticketShowing = $this->TicketShowing->find('all',array(
			'conditions'=>array(
				'TicketShowing.seat_ticket_id'=>$seat_ticket_id
			)
		));
		$this->set(compact('ticket','ticketShowing','seat_ticket_id','ticket_id'));
	}
	
	public function admin_add($ticket_id,$seat_ticket_id) {
		if ($this->request->is(array('post','put'))) {
			if ($this->TicketShowing->save($this->request->data)) {
				$this->Session->setFlash(__('The article has been saved.'));
				return $this->redirect(array('action' => 'index',$ticket_id));
			}else{
				$this->Session->setFlash(__('The article could not been saved'));
			}
		}
		
		$this->set(compact('seat_ticket_id'));
	}
	
	public function admin_edit($ticket_id,$id) {
		if ($this->request->is(array('post','put'))) {
			if ($this->TicketShowing->save($this->request->data)) {
				$this->Session->setFlash(__('The article has been saved.'));
				return $this->redirect(array('action' => 'index',$ticket_id));
			} else {
				$this->Session->setFlash(__('The seat type could not be saved. Please, try again.'));
			}
		}else{
			$this->request->data = $this->TicketShowing->findById($id);
		}
	}
}